﻿using AntdUI;
using Microsoft.Office.Interop.Excel;
using PPSMS_BLL;
using Sunny.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace PPSMS.Controllers
{
    public partial class TrendsControl : UserControl
    {
        private TrendsControl_BLL inverterStatistics_BLL = new();

        public int? ID { get; set; }

        public TrendsControl()
        {
            InitializeComponent();
        }

        private void TrendsControl_Load(object sender, EventArgs e)
        {
            // ID = null;
            uiDatetimePicker1.Value = DateTime.Now;
        }

        private void Btn_Hour_Click_1(object sender, EventArgs e)
        {
            ClinkSelect((UIButton)sender);
            string date = uiDatetimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
            Dictionary<string, double> dictionary = new();
            if (ID != null)
            {
                label_f.Visible = false;
                label_d.Text = "功率";
                dictionary = inverterStatistics_BLL.SelectMinutePower(date, ID.Value);
                CreateChart(dictionary, "分钟");
            }
            else
            {
                label_f.Visible = true;
                label_d.Visible = true;
                label_d.Text = "发电量";
                dictionary = inverterStatistics_BLL.SelectHourIrradiance(date);
                var dictionary2 = inverterStatistics_BLL.SelectHourPowerGeneration(date);
                CreateChart(dictionary, dictionary2, "时");

            }
        }

        private void Btn_Day_Click(object sender, EventArgs e)
        {
            ClinkSelect((UIButton)sender);
            string date = uiDatetimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
            Dictionary<string, double> dictionary;
            if (ID != null)
            {
                label_f.Visible = false;
                label_d.Text = "功率";
                dictionary = inverterStatistics_BLL.SelectHourPower(date, ID.Value);
                CreateChart(dictionary, "时");
            }
            else
            {
                label_f.Visible = true;
                label_d.Visible = true;
                label_d.Text = "发电量";
                dictionary = inverterStatistics_BLL.SelectDayIrradiance(date);
                var dictionary2 = inverterStatistics_BLL.SelectDayPowerGeneration(date);
                CreateChart(dictionary, dictionary2, "日");

            }
        }

        private void Btn_Month_Click(object sender, EventArgs e)
        {
            ClinkSelect((UIButton)sender);
            string date = uiDatetimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
            Dictionary<string, double> dictionary = new();
            if (ID != null)
            {
                label_f.Visible = false;
                label_d.Text = "功率";
                var dictionary1 = inverterStatistics_BLL.SelectDayPower(date, ID.Value);
                foreach (var item in dictionary1)
                {
                    dictionary.Add(item.Key.ToString(), item.Value);
                }
                CreateChart(dictionary, "日");
            }
            else
            {
                label_f.Visible = true;
                label_d.Visible = true;
                label_d.Text = "发电量";
                dictionary = inverterStatistics_BLL.SelectMonthIrradiance(date);
                var dictionary2 = inverterStatistics_BLL.SelectMonthPowerGeneration(date);
                var dictionary3 = new Dictionary<string, double>();
                foreach (var item in dictionary2)
                {
                    double avg = item.Value;
                    if (item.Key == "12")
                    {
                        avg /= 12;
                    }
                    dictionary3.Add(item.Key.ToString(), avg);

                }
                CreateChart(dictionary, dictionary3, "月");
            }

        }

        private void Btn_Year_Click(object sender, EventArgs e)
        {
            ClinkSelect((UIButton)sender);
            string date = uiDatetimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
            Dictionary<string, double> dictionary;
            if (ID != null)
            {
                label_f.Visible = false;
                label_d.Text = "功率";
                dictionary = inverterStatistics_BLL.SelectMonthPower(date, ID.Value);
                CreateChart(dictionary, "月");
            }
            else
            {
                label_d.Text = "发电量";
                label_d.Visible = true;
                label_f.Visible = false;


                date = "ALL";
                dictionary = inverterStatistics_BLL.SelectYearIrradiance(date);
                var dictionary2 = inverterStatistics_BLL.SelectYearPowerGeneration(date);
                CreateChart(dictionary, dictionary2, "年");

            }
        }

        private void CreateChart(Dictionary<string, double> dictionary1, Dictionary<string, double> dictionary2, string company)
        {
            var option = new UILineOption();
            option.ToolTip.Visible = true;
            option.Title = new UITitle();
            option.Title.Text = "电量辐照度趋势图";
            option.Title.SubText = "发电量和辐照度";

            var series1 = option.AddSeries(new UILineSeries(company + "辐照度", Color.Red));
            var series2 = option.AddSeries(new UILineSeries(company + "发电量", Color.Blue, true));
            // 设置X轴坐标轴为自定义标签
            if (company == "年")
            {
                option.XAxis.CustomLabels = new CustomLabels(1, 1, dictionary2.Count - 1);

                series2.Add(0, 0);
                foreach (var item in dictionary2)
                {
                    //string key = (int.Parse(item.Key)).ToString();
                    option.XAxis.CustomLabels.AddLabel(item.Key + company);
                    series2.Add(item.Key, item.Value);
                }

                // 设置Y轴坐标轴范围
                uiLineChart1.Option.YAxis.SetRange(0, 10);

                series2.YAxisDecimalPlaces = 0;

                series2.Symbol = UILinePointSymbol.Circle;
                series2.SymbolSize = 4;
                series2.SymbolLineWidth = 1;
                series2.SymbolColor = Color.Red; // 可以选择不同颜色以区分

                option.XAxis.Name = $"时间({company})";
                option.Y2Axis.Name = "千瓦时(kWh)";

                uiLineChart1.SetOption(option);

            }
            else
            {
                option.XAxis.CustomLabels = new CustomLabels(1, 1, Math.Max(dictionary1.Count, dictionary2.Count) - 1);
                series1.Add(0, 0);
                foreach (var item in dictionary1)
                {
                    //string key =(int.Parse(item.Key)).ToString();
                    option.XAxis.CustomLabels.AddLabel(item.Key + company);
                    series1.Add(item.Key, item.Value);
                }
                series2.Add(0, 0);
                foreach (var item in dictionary2)
                {
                    //string key = (int.Parse(item.Key)).ToString();
                    series2.Add(item.Key, item.Value);
                }

                // 设置Y轴坐标轴范围
                uiLineChart1.Option.YAxis.SetRange(0, 10000);
                series1.YAxisDecimalPlaces = 0;
                series2.YAxisDecimalPlaces = 0;

                //series1.Symbol = UILinePointSymbol.Diamond;
                //series1.SymbolSize = 4;
                //series1.SymbolLineWidth = 1;
                series1.SymbolColor = Color.Blue;

                //series2.Symbol = UILinePointSymbol.Circle;
                //series2.SymbolSize = 4;
                //series2.SymbolLineWidth = 1;
                //series2.SymbolColor = Color.Blue; // 可以选择不同颜色以区分
                series1.Smooth = true;
                series2.Smooth = true;

                option.XAxis.Name = $"时间({company})";
                option.YAxis.Name = "辐照度(lux)";
                option.Y2Axis.Name = "千瓦时(kWh)";


                uiLineChart1.SetOption(option);
            }
        }

        private void CreateChart(Dictionary<string, double> dictionary, string company)
        {

            var option = new UILineOption();
            option.ToolTip.Visible = true;
            option.Title = new UITitle();
            option.Title.Text = "逆变器工作趋势图";
            option.Title.SubText = "功率";

            var series = option.AddSeries(new UILineSeries(company + "平均功率", Color.Blue));
            //设置X轴坐标轴为自定义标签
            option.XAxis.CustomLabels = new CustomLabels(1, 1, dictionary.Count - 1);

            foreach (var item in dictionary)
            {
                //string key = (int.Parse(item.Key)).ToString();
                option.XAxis.CustomLabels.AddLabel(item.Key + company);
            }
            //设置Y轴坐标轴范围
            uiLineChart1.Option.YAxis.SetRange(0, 10000);
            series.YAxisDecimalPlaces = 2;//设置Y轴坐标轴小数位数           

            //series.Symbol = UILinePointSymbol.Circle;
            //series.SymbolSize = 2;
            //series.SymbolLineWidth = 2;
            series.SymbolColor = Color.Red;
            series.Smooth = true;//平滑曲线

            option.XAxis.Name = $"时间({company})";
            option.YAxis.Name = "功率(kW)";
            series.Add(0, 0);
            foreach (var item in dictionary)
            {
                series.Add(item.Key, item.Value);
            }
            uiLineChart1.SetOption(option);
        }
        private void ClinkSelect(UIButton uIButton)
        {
            this.Btn_Day.FillColor = this.Btn_Hour.FillColor = this.Btn_Month.FillColor = this.Btn_Year.FillColor = Color.Transparent;
            uIButton.FillColor = uIButton.FillColor == Color.FromArgb(23, 94, 119) ? Color.Transparent : Color.FromArgb(23, 94, 119);
        }
    }
}
